///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
#include <Input/InputManager.h>
#include <string.h>

//-----------------------------------------------------------------------------

InputManager::InputManager()
{
	ResetInput();
}

//-----------------------------------------------------------------------------

InputManager::~InputManager()
{
}

//-----------------------------------------------------------------------------

void	InputManager::ResetInput()
{
	//keys
	memset( m_keyDown, 0, sizeof( m_keyDown ) );
	memset( m_keyPressed, 0, sizeof( m_keyPressed ) );
	memset( m_keyReleased, 0, sizeof( m_keyReleased ) );

	memset( m_keyDown1, 0, sizeof( m_keyDown1 ) );
	memset( m_keyPressed1, 0, sizeof( m_keyPressed1 ) );
	memset( m_keyReleased1, 0, sizeof( m_keyReleased1 ) );

	//mouse
	memset( m_mouseButtonsDown, 0, sizeof( m_mouseButtonsDown ) );
	memset( m_mouseButtonsPressed, 0, sizeof( m_mouseButtonsPressed ) );
	memset( m_mouseButtonsReleased, 0, sizeof( m_mouseButtonsReleased ) );

	memset( m_mouseButtonsDown1, 0, sizeof( m_mouseButtonsDown1 ) );
	memset( m_mouseButtonsPressed1, 0, sizeof( m_mouseButtonsPressed1 ) );
	memset( m_mouseButtonsReleased1, 0, sizeof( m_mouseButtonsReleased1 ) );

	m_mouseX = m_mouseY = m_mouseXOffset = m_mouseYOffset = 0;
}

//-----------------------------------------------------------------------------

void	InputManager::Update()
{
	//keys
	memcpy( m_keyDown, m_keyDown1, sizeof( m_keyDown ) );
	memcpy( m_keyPressed, m_keyPressed1, sizeof( m_keyPressed ) );
	memcpy( m_keyReleased, m_keyReleased1, sizeof( m_keyReleased ) );
	memset( m_keyPressed1, 0, sizeof( m_keyPressed1 ) );
	memset( m_keyReleased1, 0, sizeof( m_keyReleased1 ) );

	//mouse
	memcpy( m_mouseButtonsDown, m_mouseButtonsDown1, sizeof( m_mouseButtonsDown ) );
	memcpy( m_mouseButtonsPressed, m_mouseButtonsPressed1, sizeof( m_mouseButtonsPressed ) );
	memcpy( m_mouseButtonsReleased, m_mouseButtonsReleased1, sizeof( m_mouseButtonsReleased ) );
	memset( m_mouseButtonsPressed1, 0, sizeof( m_mouseButtonsPressed1 ) );
	memset( m_mouseButtonsReleased1, 0, sizeof( m_mouseButtonsReleased1 ) );

	//m_mouseXOffset = m_mouseYOffset = 0;
}

//-----------------------------------------------------------------------------

bool	InputManager::IsKeyDown( u8 key )
{
	ASSERT( key >= 0 && key < KEY_VECTOR_SIZE );
	return m_keyDown[ key ];
}

//-----------------------------------------------------------------------------

bool	InputManager::KeyPressed( u8 key )
{
	ASSERT( key >= 0 && key < KEY_VECTOR_SIZE );
	return m_keyPressed[ key ];
}

//-----------------------------------------------------------------------------

bool	InputManager::KeyReleased( u8 key )
{
	ASSERT( key >= 0 && key < KEY_VECTOR_SIZE );
	return m_keyReleased[ key ];
}

//-----------------------------------------------------------------------------

void	InputManager::GetInput( bool down, u8 key, int x, int y )
{
	ASSERT( key >= 0 && key < KEY_VECTOR_SIZE );
	m_keyDown1[ key ] = down;
	bool lastState = m_keyDown[ key ];
	m_keyPressed1[ key ] = down && lastState != down;
	m_keyReleased1[ key ] = !down && lastState != down;
}

//-----------------------------------------------------------------------------

void	InputManager::GetMouseClick( s32 button, bool state, s32 x, s32 y )
{
	ASSERT( button >= 0 && button < MOUSE_BUTTONS );

	m_mouseButtonsDown1[ button ] = state;
	bool lastState = m_mouseButtonsDown[ button ];
	m_mouseButtonsPressed1[ button ] = state && lastState != state;
	m_mouseButtonsReleased1[ button ] = !state && lastState != state;

	m_mouseX = x;
	m_mouseY = y;
	m_mouseXOffset = m_mouseYOffset = 0;
	GetMouseMove( x, y );
}

//-----------------------------------------------------------------------------

void	InputManager::GetMouseMove( s32 x, s32 y )
{
	m_mouseXOffset += x - m_mouseX;
	m_mouseYOffset += y - m_mouseY;
	m_mouseX = x;
	m_mouseY = y;
}

//-----------------------------------------------------------------------------

bool	InputManager::HasMouseMoved()
{
	return m_mouseXOffset != 0 || m_mouseYOffset != 0;
}

//-----------------------------------------------------------------------------

s32		InputManager::GetMousePositionX()
{
	return m_mouseX;
}

//-----------------------------------------------------------------------------

s32		InputManager::GetMousePositionY()
{
	return m_mouseY;
}

//-----------------------------------------------------------------------------

s32		InputManager::GetMouseOffsetX()
{
	s32 save = m_mouseXOffset;
	m_mouseXOffset = 0;
	return save;
}

//-----------------------------------------------------------------------------

s32		InputManager::GetMouseOffsetY()
{
	s32 save = m_mouseYOffset;
	m_mouseYOffset = 0;
	return save;
}

//-----------------------------------------------------------------------------

bool	InputManager::IsMouseButtonDown( s32 button )
{
	ASSERT( button >= 0 && button < MOUSE_BUTTONS );
	return m_mouseButtonsDown[ button ];
}

//-----------------------------------------------------------------------------

bool	InputManager::IsMouseButtonPressed( s32 button )
{
	ASSERT( button >= 0 && button < MOUSE_BUTTONS );
	return m_mouseButtonsPressed[ button ];
}

//-----------------------------------------------------------------------------

bool	InputManager::IsMouseButtonReleased( s32 button )
{
	ASSERT( button >= 0 && button < MOUSE_BUTTONS );
	return m_mouseButtonsReleased[ button ];
}

//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
